package com.bangdream.michelia.view.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.bangdream.michelia.R;
import com.bangdream.michelia.application.MainApplication;
import com.bangdream.michelia.entity.CoursesTypeBean;
import com.bangdream.michelia.utils.L;

import java.util.ArrayList;
import java.util.List;

/**
 * @date on 2018/6/20 0:07
 * @email 909732184@qq.com
 * 侧边栏的列表适配器
 */
public class SlideAdapter extends RecyclerView.Adapter {
    private Context context;
    private LayoutInflater layoutInflater;
    private OnSlideClickListener onSlideClickListener;
    private List<CoursesTypeBean> data;
    public OnSlideClickListener getOnSlideClickListener() {
        return onSlideClickListener;
    }

    public void setOnSlideClickListener(OnSlideClickListener onSlideClickListener) {
        this.onSlideClickListener = onSlideClickListener;
    }

    public SlideAdapter(Context context) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        data = new ArrayList<>();
    }

    public void setData(List<CoursesTypeBean> data) {
        this.data = data;
    }

    public List<CoursesTypeBean> getData() {
        return data;
    }

    public interface OnSlideClickListener{
        void onSlideItemClick(int position);
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(layoutInflater.inflate(R.layout.slide_adpater_item,parent,false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder viewHolder = null;
        if(holder instanceof ViewHolder){
            viewHolder = (ViewHolder) holder;
            viewHolder.position = position;
            viewHolder.tvText.setText(data.get(position).getName());
        }
    }

    @Override
    public int getItemCount() {
        return data == null?0:data.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        private int position;
        private TextView tvText;
        public ViewHolder(View itemView) {
            super(itemView);
            tvText = itemView.findViewById(R.id.tvText);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(onSlideClickListener!=null){
                        onSlideClickListener.onSlideItemClick(position);
                    }
                }
            });
        }
    }
}
